//package com.ruoyi.oar.controller.pc;
//
//import cn.hutool.http.HttpUtil;
//import com.alibaba.fastjson.JSONObject;
//import com.ruoyi.common.constant.Constants;
//import com.ruoyi.common.core.domain.AjaxResult;
//import com.ruoyi.common.core.domain.entity.SysDept;
//import com.ruoyi.common.core.domain.entity.SysUser;
//import com.ruoyi.common.core.redis.RedisCache;
//import com.ruoyi.common.core.redis.RedisConstants;
//import com.ruoyi.common.exception.ServiceException;
//import com.ruoyi.common.utils.ToolUtil;
//import com.ruoyi.common.utils.sm4.SM4Utils;
//import com.ruoyi.framework.web.service.SysLoginService;
//import com.ruoyi.oar.api.dto.UserPlice;
//import com.ruoyi.oar.api.dto.UserPliceResult;
//import com.ruoyi.system.service.ISysDeptService;
//import com.ruoyi.system.service.ISysUserService;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiParam;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//
///**
// * 登录验证
// *
// * @author ruoyi
// */
//@Slf4j
//@Api(tags = "pc登陆")
//@RestController
//@RequestMapping("/pc")
//public class PcLoginController {
//    public static String url;
//
//    @Value("${police.url}")
//    public void setUrl(String url) {
//        PcLoginController.url = url;
//        log.info("获取民警登陆账号url={}", url);
//    }
//
//    @Autowired
//    private SysLoginService loginService;
//    @Autowired
//    private ISysUserService sysUserService;
//    @Autowired
//    private ISysDeptService sysDeptService;
//    @Autowired
//    private RedisCache redisCache;
//
//    /**
//     * 小程序解密手机登陆
//     *
//     * @param policeNumber 警号
//     * @return 结果
//     */
//    @ApiOperation("PC登陆")
//    @PostMapping("/login")
//    public AjaxResult login(@RequestParam(required = false) @ApiParam("警号") String policeNumber) {
//        if (ToolUtil.isEmpty(policeNumber)) {
//            throw new ServiceException("警号不能为空");
//        }
//        policeNumber = SM4Utils.decryptData_ECB(policeNumber);//解密
//        SysUser sysUser = sysUserService.selectUserByUserName(policeNumber);
//        if (ToolUtil.isEmpty(sysUser)) {
//            //创建用户
//            String result = HttpUtil.get(url, ToolUtil.mapUtil("policeNumber", policeNumber));
//            UserPliceResult pliceResult = JSONObject.parseObject(result, UserPliceResult.class);
//            if (ToolUtil.isEmpty(pliceResult) || !"200".equals(pliceResult.getCode()) || ToolUtil.isEmpty(pliceResult.getData())) {
//                throw new ServiceException("查不到民警账户信息，请联系管理员核查");
//            }
//            //添加用户跟角色
//            redisCache.lockExecute(RedisConstants.REDIS_USER_ADD_KEY + policeNumber, (lockId) -> {
//                UserPlice data = pliceResult.getData();
//                SysUser sysUserNew = new SysUser();
//                sysUserNew.setPhonenumber(data.getUserPhone());
//                sysUserNew.setUserName(data.getPoliceNumber());
//                sysUserNew.setUserType("1");
//                sysUserNew.setName(data.getUserName());
//                sysUserNew.setCardId(data.getCertNo());
//                sysUserNew.setStatus("0");
//                sysUserNew.setDelFlag("0");
//                sysUserNew.setNickName(data.getUserName());
//                if (ToolUtil.isNotEmpty(data.getGonganCode())) {
//                    SysDept sysDept = sysDeptService.selectDeptById(data.getGonganCode());
//                    if (ToolUtil.isNotEmpty(sysDept)) {
//                        sysUserNew.setDeptId(sysDept.getDeptId());
//                    }
//                }
//                sysUserNew.setRoleIds(new Long[]{2L}); //2是民警
//                sysUserService.insertUser(sysUserNew);
//                return true;
//            });
//        }
//        AjaxResult ajax = AjaxResult.success();
//        //获取手机号
//        String token = loginService.pliceUserLogin(policeNumber);
//        // 生成token
//        ajax.put(Constants.TOKEN, token);
//        return ajax;
//    }
//
//    public static void main(String[] args) {
//        String result = HttpUtil.get("http://183.252.15.71:963/other/xmsso/getUserInfoByPoliceNumber", ToolUtil.mapUtil("policeNumber", "000000"));
//        UserPliceResult pliceResult = JSONObject.parseObject(result, UserPliceResult.class);
//        System.out.println(result);
//    }
//}
